<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有!                                |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Joey<shaoshuai@clcw.com.cn>                                     |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Article;

/**
 * 资讯管理
 * Class Article
 *
 * @author shaoshuai
 */
class Article
{
    /**
     * 获取子分类
     *
     * @param number $pid
     */
    public static function getCategoryByPid($pid)
    {
        return M('article_category', 'clcw_', 'DB_CLCW')->where("parent_id = '$pid' AND status = 1")->select();
    }

    /**
     * 获取文章分类
     */
    public static function getCategory()
    {
        return M('article_category', 'clcw_', 'DB_CLCW')->where('status = 1')->select();
    }

    /**
     * 获取文章内容
     *
     * @param number $id
     */
    public static function getArticleByID($id)
    {
        return M('article', 'clcw_', 'DB_CLCW')->where('article_id=' . $id)->find();
    }

    /**
     * 保存文章
     *
     * @param unknown $data
     *
     * @return boolean|mixed|boolean|unknown|string
     */
    public static function saveArticle($data)
    {
        $id = $data['id'];
        unset($data['id']);

        if ($id) {
            $data['update_time'] = NOW_TIME;
            return M('article', 'clcw_', 'DB_CLCW')->where(['article_id' => $id])->save($data);
        } else {
            $data['status'] = 2;
            $data['create_time'] = NOW_TIME;
            return M('article', 'clcw_', 'DB_CLCW')->add($data);
        }
    }

    /**
     * 获取文章列表
     *
     * @param array  $params
     * @param number $page
     * @param number $pageSize
     *
     * @return array
     */
    public static function getArticleList($params, $page, $pageSize)
    {

        if (isset($params['status'])) {
            $condition['status'] = $params['status'];
        }

        if ($params['category_id']) {
            $condition['category_id'] = $params['category_id'];
        }

        if ($params['child_id']) {
            $condition['child_id'] = $params['child_id'];
        }

        if (isset($params['is_top'])) {
            $condition['is_top'] = $params['is_top'];
        }
        if ($params['type']) {
            $condition['type'] = $params['type'];
        }
        if ($params['from_name']) {
            $condition['from_name'] = $params['from_name'];
        }

        if ($params['article_title']) {
            $condition['article_title'] = ['like', "%{$params['article_title']}%"];
        }

        $pageSize = 10;

        $list = M('article', 'clcw_', 'DB_CLCW')
            ->where($condition)
            ->order('`article_id` DESC')
            ->page($page, $pageSize)
            ->select();
        $total = M('article', 'clcw_', 'DB_CLCW')
            ->where($condition)
            ->count();

        return ['total' => $total, 'list' => $list];

    }

    /**
     * 获取牛人列表
     */
    public static function getExpertUserList()
    {
        return M('user', 'clcw_', 'DB_CLCW')->field('user_id,nick_name,mobile')->where(['is_expert' => 1])->select();
    }

    /**
     * 获取牛人日报列表
     *
     * @param unknown $params
     * @param unknown $page
     * @param unknown $pageSize
     */
    public static function getExpertArticleList($params, $page, $pageSize)
    {

        if (isset($params['status'])) {
            $condition['status'] = $params['status'];
        }

        if ($params['article_title']) {
            $condition['article_title'] = ['like', "%{$params['article_title']}%"];
        }

        $list = M('expert_article', 'clcw_', 'DB_CLCW')
            ->where($condition)
            ->order('`article_id` DESC')
            ->page($page,$pageSize)
            ->select();
        $total = M('expert_article', 'clcw_', 'DB_CLCW')
            ->where($condition)
            ->count();

        return ['total' => $total, 'list' => $list];

    }

    /**
     * 获取牛人日报
     *
     * @param unknown $id
     */
    public static function getExpertArticleByID($id)
    {
        return M('expert_article', 'clcw_', 'DB_CLCW')->where('article_id=' . $id)->find();
    }

    public static function saveExpertArticle($data)
    {
        $id = $data['id'];
        unset($data['id']);

        if ($id) {
            return M('expert_article', 'clcw_', 'DB_CLCW')->where(['article_id' => $id])->save($data);
        } else {
            $data['status'] = 2;
            $data['create_time'] = NOW_TIME;
            return M('expert_article', 'clcw_', 'DB_CLCW')->add($data);
        }

    }

    /**
     * 获取牛人日报作者
     */
    public function getExpertAuthorById($userId)
    {

        return M('user', 'clcw_', 'DB_CLCW')->where(['user_id' => $userId])->find();

    }
}